package adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.administrator.text.R;
import java.util.List;
import entity.HomeZhuYe;
import util.BitmapUtil;
import util.JumpActivityUtils;
import util.LogUtil;

/**
 * Created by Administrator on 2016/5/19.
 *首页ViewPager
 */
public class Gridadapter extends BaseAdapter {
    private LayoutInflater mInflater;
    private List<HomeZhuYe.HotSiteModelsBean> mListBtton;
    private List<HomeZhuYe.AdModelsBean> mAdModelsBean;
    private Context mContext;
    private boolean isOnMeasure = true; //position == 0 时会加载多次
    private int mChoice;        //区分不同项目 1显示网站 2 就显示图片
    public Gridadapter(Context context, List<HomeZhuYe.HotSiteModelsBean> listBtton,List<HomeZhuYe.AdModelsBean> adModelsBean,int choice){
        this.mContext = context;
        this.mInflater = LayoutInflater.from(context);
        this.mListBtton = listBtton;
        this.mAdModelsBean = adModelsBean;
        this.mChoice = choice;

    }
    @Override public int getCount() {
        if(mChoice == 1){
            return mListBtton.size();
        }else {
            return mAdModelsBean.size();
        }
    }

    @Override public Object getItem(int position) {
        return position;
    }

    @Override public long getItemId(int position) {
        return position;
    }

    @Override public View getView(final int position, View convertView, ViewGroup parent) {
        if(mChoice == 1){
            ViewHolder holder;
            if (convertView==null) {
                holder=new ViewHolder();
                convertView = mInflater.inflate(R.layout.adapter_grida, null);
                holder.iv=(ImageView) convertView.findViewById(R.id.main_grid_item_iv);
                holder.tv=(TextView) convertView.findViewById(R.id.main_grid_item_tv);
                convertView.setTag(holder);
            } else {
                holder=(ViewHolder) convertView.getTag();
            }
            if(position == 0){
                if(isOnMeasure){
                    isOnMeasure = false;
                    BitmapUtil.newBitmap().showBitmap(mContext,mListBtton.get(position).getPic(),holder.iv,R.mipmap.homepage_icon,null);
                    holder.tv.setText(mListBtton.get(position).getText());
                }
            }else {
                BitmapUtil.newBitmap().showBitmap(mContext,mListBtton.get(position).getPic(),holder.iv,R.mipmap.homepage_icon,null);
                holder.tv.setText(mListBtton.get(position).getText());
            }

            convertView.setOnClickListener(new View.OnClickListener() {
                @Override public void onClick(View v) {
                    JumpActivityUtils.getInstance().jumpWebView(mContext,mListBtton.get(position).getUrl(),mListBtton.get(position).getText());
                }
            });
            return convertView;
        }else if(mChoice == 2){
            LogUtil.eE("",mAdModelsBean+"....................mAdModelsBean");
            ViewHolderIM viewHolderIm;
            if(convertView == null){
                viewHolderIm=new ViewHolderIM();
                convertView = mInflater.inflate(R.layout.adapter_welfare_buttoniv, null);
                viewHolderIm.iv=(ImageView) convertView.findViewById(R.id.adapter_welfare_iv);
                convertView.setTag(viewHolderIm);
            }else {
                viewHolderIm=(ViewHolderIM) convertView.getTag();
            }

            BitmapUtil.newBitmap().showBitmap(mContext,mAdModelsBean.get(position).getPic(),viewHolderIm.iv,R.mipmap.homepage_icon,null);
            convertView.setOnClickListener(new View.OnClickListener() {
                @Override public void onClick(View v) {
                    JumpActivityUtils.getInstance().jumpWebView(mContext,mAdModelsBean.get(position).getUrl(),mAdModelsBean.get(position).getText());
                }
            });
            return convertView;
        }
        return convertView;
    }

    private static class ViewHolder{
        ImageView iv;
        TextView tv;
    }

    private static class ViewHolderIM{
        ImageView iv;
    }
}
